"""
思路分析:
1.创建一个类
1.1 定义一个初始化方法
    1.1.1 初始化属性获得全部测试数据
    1.1.2 获取当前测试数据
    1.1.3 获取返回结果
    1.1.4 获取当前测试数据的断言字段并存为字典
1.2 定义一个对外的断言方法
    1.2.1 循环获取断言字典的键值对
    1.2.2 根据取出的键值对获取返回结果里面的对应结果
    1.2.3 断言取到的实际结果和预期结果是否一致
"""
from jsonpath import jsonpath
import unittest
from common.configHttp import configHttp

class PublicAssertion(unittest.TestCase):
    def __init__(self,id,data,re):
        super().__init__()
        self.data = data[int(id)-1]
        print("测试数据--->",self.data)
        self.re = re.json()
        self.assert_data = {key:value for key,value in self.data.items() if key.startswith("expect")}

    def public_assert(self):
        for v in self.assert_data.values():
            res = v.split(":")
            if len(res) > 1:
                real = jsonpath(self.re,"$.." + res[0])[0]
                real = str(real)
                self.assertEqual(real,res[1])




if __name__ == '__main__':
    testdata = [{'id': '1', 'interfaceUrl': 'https://www.wanandroid.com/user/login', 'name': 'login', 'method': 'post', 'value': "{'username':'liangchao','password':'123456'}", 'header': '{}', 'expect1': 'errorCode:0', 'expect2': 'username:liangchao1'}, {'id': '2', 'interfaceUrl': 'https://www.wanandroid.com/user/register', 'name': 'register', 'method': 'post', 'value': "{'username':'liangchao03','password':'123456','repassword':'123456'}", 'header': '{}', 'expect1': 'errorCode:0', 'expect2': ''}, {'id': '3', 'interfaceUrl': 'https://www.wanandroid.com/user/logout/json', 'name': 'logout', 'method': 'get', 'value': "{'username':'liangchao'}", 'header': '{}', 'expect1': 'errorCode:0', 'expect2': ''}]
    method = "post"
    url = "https://www.wanandroid.com/user/login"
    value = "{'username': 'liangchao', 'password': '123456'}"
    header ="{}"

    #实例化
    ch = configHttp(method,url,value,header)
    re = ch.run()
    print(re.text)

    #实例化
    pa = PublicAssertion("1",testdata,re)
    pa.public_assert()













